package com.koron.css2.contractmanage.impl;

import com.koron.css2.ServerInterface;
import com.koron.css2.contractmanage.Enum.ContractStatusEnum;
import com.koron.css2.contractmanage.Enum.ContractTypeEnum;
import com.koron.css2.contractmanage.bean.ContractManageBean;
import com.koron.css2.contractmanage.exception.ContractException;
import com.koron.css2.contractmanage.mapper.ContractManageMapper;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.serviceManage.utils.JsonUtils;
import com.koron.util.Constant;
import org.apache.commons.lang3.StringUtils;
import org.koron.ebs.mybatis.SessionFactory;
import org.swan.bean.MessageBean;


/**
 * 合同签约完成
 *
 * @date 2023/4/17 10:03
 */
public class ContractComplete implements ServerInterface {

    @Override
    public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {
        try {
            ContractManageMapper mapper = factory.getMapper(ContractManageMapper.class);
            ContractManageBean bean = JsonUtils.objectToPojo(req.getData(), ContractManageBean.class);
            if (bean == null) {
                throw new ContractException("合同签约完成请求参数不能为空！");
            }
            //校验合同字段
            checkContractManageBean(bean.getProjectId(), bean.getContractNo(), bean.getContractType(), bean.getContractStatus(), userInfo);

            //更新合同表状态
            int updateContract = updateContract(bean, userInfo, mapper);

            if (updateContract < 1) {
                throw new ContractException("更新合同签约完成状态失败！");
            }
            return MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "签约完成成功", void.class);
        } catch (Exception e) {
            logger.error("签约完成失败: " + e.getMessage(), e);
            e.printStackTrace();
            return MessageBean.create(Constant.MESSAGE_INT_FAIL, "签约完成失败: " + e.getMessage(), null);
        }
    }

    /**
     * 校验合同字段
     *
     * @param projectId
     * @param contractType
     * @return
     */
    public void checkContractManageBean(String projectId, String contractNo, String contractType, String contractStatus, UserInfoBean userInfo) {
        if (userInfo == null) {
            throw new ContractException("用户信息不能为空！");
        }
        if (StringUtils.isBlank(projectId)) {
            throw new ContractException("参数中的报装单ID不能为空！");
        }
        if (StringUtils.isBlank(contractNo)) {
            throw new ContractException("参数中的合同编码不能为空！");
        }
        if (StringUtils.isBlank(contractType)) {
            throw new ContractException("参数中的合同类型不能为空！");
        }
        if (StringUtils.isBlank(contractStatus)) {
            throw new ContractException("参数中的合同状态不能为空！");
        }
        if (ContractTypeEnum.CONTRACT_TYPE_2.getContractType().equals(contractType)) {
            throw new ContractException("电子合同请通过电子签章平台签约完成！");
        }

        if (!ContractStatusEnum.CONTRACT_STATUS_7.getContractStatus().equals(contractStatus)) {
            //如果合同状态为7，则可签约完成
            throw new ContractException("只有已上传合同附件的合同才能签约完成！");
        }
    }

    /**
     * 更新合同表签约成功状态
     *
     * @param bean
     * @param userInfo
     * @param mapper
     * @return
     */
    public int updateContract(ContractManageBean bean, UserInfoBean userInfo, ContractManageMapper mapper) {
        ContractManageBean updateContractBean = new ContractManageBean();
        updateContractBean.setProjectId(bean.getProjectId());
        updateContractBean.setContractNo(bean.getContractNo());
        updateContractBean.setContractStatus(ContractStatusEnum.CONTRACT_STATUS_2.getContractStatus());
        updateContractBean.setUpdateInfo(userInfo);
        int result = mapper.updateContract(updateContractBean);
        return result;
    }

}

